/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmIndex.java
 *
 * Created on 10-ago-2011, 11:44:07
 */
/**
 *
 * @author Kid_goth
 */
import java.io.BufferedReader;
import java.io.FileReader;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import org.edisoncor.gui.util.WindowsUtil;
public class frmIndex extends javax.swing.JFrame {

    String ubi = "";
    String hash = "";
    String cadena = "";
    String subExt = "";
    int descifrado = 0;
    clsEncript enc = new clsEncript();
    /** Creates new form frmIndex */
    public frmIndex() 
    {
        System.out.print(enc.Crypt("asdasdas"));
        initComponents();
        this.setSize(386, 425);
        this.setLocationRelativeTo(null);
        WindowsUtil.makeWindowsOpacity(this, .90f);
        WindowsUtil.makeWindowsShape(this, null);
        Limpiar();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblSal = new javax.swing.JLabel();
        lblMin = new javax.swing.JLabel();
        lblTit = new javax.swing.JLabel();
        txtHash = new org.edisoncor.gui.textField.TextFieldRoundIcon();
        lblDicc = new org.edisoncor.gui.label.LabelMetric();
        lblHash = new org.edisoncor.gui.label.LabelMetric();
        txtDic = new org.edisoncor.gui.textField.TextFieldRoundIcon();
        btnAbr = new org.edisoncor.gui.button.ButtonSeven();
        btnOpe = new org.edisoncor.gui.button.ButtonSeven();
        btnLim = new org.edisoncor.gui.button.ButtonSeven();
        cbxOpc = new javax.swing.JComboBox();
        lblADe = new javax.swing.JLabel();
        lblAn = new javax.swing.JLabel();
        lblDes = new javax.swing.JLabel();
        lblProc = new javax.swing.JLabel();
        lblp = new javax.swing.JLabel();
        lblBor = new javax.swing.JLabel();
        lblFondo = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("J - MD5_Decrypt");
        setBackground(new java.awt.Color(102, 0, 0));
        setUndecorated(true);
        getContentPane().setLayout(null);

        lblSal.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/cerr.png"))); // NOI18N
        lblSal.setText("jLabel1");
        lblSal.setToolTipText("Cerrar");
        lblSal.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                evtCerrar(evt);
            }
        });
        getContentPane().add(lblSal);
        lblSal.setBounds(10, 10, 20, 20);

        lblMin.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/min.png"))); // NOI18N
        lblMin.setToolTipText("Minimizar");
        lblMin.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                evtMinimizar(evt);
            }
        });
        getContentPane().add(lblMin);
        lblMin.setBounds(30, 10, 20, 19);

        lblTit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/titulo.png"))); // NOI18N
        lblTit.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                lblTitMouseDragged(evt);
            }
        });
        getContentPane().add(lblTit);
        lblTit.setBounds(0, -10, 400, 70);

        txtHash.setBackground(new java.awt.Color(0, 0, 0));
        txtHash.setForeground(new java.awt.Color(153, 153, 153));
        txtHash.setFont(new java.awt.Font("Tahoma", 1, 12));
        txtHash.setSelectedTextColor(new java.awt.Color(204, 0, 0));
        txtHash.setSelectionColor(new java.awt.Color(0, 0, 0));
        getContentPane().add(txtHash);
        txtHash.setBounds(20, 90, 340, 30);

        lblDicc.setForeground(new java.awt.Color(204, 204, 204));
        lblDicc.setText("Elije tu Diccionario:");
        getContentPane().add(lblDicc);
        lblDicc.setBounds(30, 150, 220, 17);

        lblHash.setForeground(new java.awt.Color(204, 204, 204));
        lblHash.setText("Introduce el Hash MD5:");
        getContentPane().add(lblHash);
        lblHash.setBounds(30, 70, 220, 17);

        txtDic.setBackground(new java.awt.Color(0, 0, 0));
        txtDic.setEditable(false);
        txtDic.setForeground(new java.awt.Color(153, 153, 153));
        txtDic.setFont(new java.awt.Font("Tahoma", 1, 12));
        txtDic.setSelectedTextColor(new java.awt.Color(204, 0, 0));
        txtDic.setSelectionColor(new java.awt.Color(0, 0, 0));
        txtDic.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                txtDicMouseReleased(evt);
            }
        });
        getContentPane().add(txtDic);
        txtDic.setBounds(20, 170, 250, 30);

        btnAbr.setBackground(new java.awt.Color(153, 0, 0));
        btnAbr.setText("Examinar");
        btnAbr.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAbrActionPerformed(evt);
            }
        });
        getContentPane().add(btnAbr);
        btnAbr.setBounds(250, 170, 110, 32);

        btnOpe.setBackground(new java.awt.Color(153, 0, 0));
        btnOpe.setForeground(new java.awt.Color(204, 204, 204));
        btnOpe.setText("Decrypt It!");
        btnOpe.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOpeActionPerformed(evt);
            }
        });
        getContentPane().add(btnOpe);
        btnOpe.setBounds(50, 230, 116, 35);

        btnLim.setBackground(new java.awt.Color(153, 0, 0));
        btnLim.setForeground(new java.awt.Color(204, 204, 204));
        btnLim.setText("Limpiar Todo");
        btnLim.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLimActionPerformed(evt);
            }
        });
        getContentPane().add(btnLim);
        btnLim.setBounds(200, 230, 116, 35);

        cbxOpc.setBackground(new java.awt.Color(0, 0, 0));
        cbxOpc.setForeground(new java.awt.Color(204, 204, 204));
        cbxOpc.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "No Funciono??" }));
        cbxOpc.setToolTipText("Convierte las palabras del diccionario a lo que elijas...");
        cbxOpc.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(102, 0, 51), 1, true));
        getContentPane().add(cbxOpc);
        cbxOpc.setBounds(120, 280, 130, 22);

        lblADe.setFont(new java.awt.Font("Tahoma", 1, 18));
        lblADe.setForeground(new java.awt.Color(204, 204, 204));
        lblADe.setText("?");
        lblADe.setToolTipText("Acerca De:");
        lblADe.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                evtAcercaDe(evt);
            }
        });
        getContentPane().add(lblADe);
        lblADe.setBounds(10, 400, 20, 20);

        lblAn.setFont(new java.awt.Font("Consolas", 0, 12));
        lblAn.setForeground(new java.awt.Color(204, 204, 204));
        lblAn.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        lblAn.setText("Hash Descifrado: ");
        getContentPane().add(lblAn);
        lblAn.setBounds(30, 350, 140, 20);

        lblDes.setFont(new java.awt.Font("Consolas", 0, 12)); // NOI18N
        lblDes.setForeground(new java.awt.Color(204, 204, 204));
        lblDes.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblDes.setText("No Encontrado...");
        lblDes.setToolTipText("");
        lblDes.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                lblDesMouseReleased(evt);
            }
        });
        getContentPane().add(lblDes);
        lblDes.setBounds(160, 350, 200, 20);

        lblProc.setFont(new java.awt.Font("Consolas", 0, 12));
        lblProc.setForeground(new java.awt.Color(204, 204, 204));
        lblProc.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblProc.setText("Esperando Inicio");
        getContentPane().add(lblProc);
        lblProc.setBounds(160, 310, 200, 20);

        lblp.setFont(new java.awt.Font("Consolas", 0, 12));
        lblp.setForeground(new java.awt.Color(204, 204, 204));
        lblp.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        lblp.setText("Proceso Actual...");
        getContentPane().add(lblp);
        lblp.setBounds(30, 310, 140, 20);

        lblBor.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/bordes.png"))); // NOI18N
        lblBor.setToolTipText("by Kid_Goth");
        getContentPane().add(lblBor);
        lblBor.setBounds(0, 0, 390, 430);

        lblFondo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/fondo.png"))); // NOI18N
        getContentPane().add(lblFondo);
        lblFondo.setBounds(0, -10, 390, 440);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void lblTitMouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblTitMouseDragged
        this.setLocation
        (
            this.getX() + evt.getX() - lblTit.getWidth() / 2,
            this.getY() + evt.getY() - lblTit.getHeight() / 2
        );         // TODO add your handling code here:
    }//GEN-LAST:event_lblTitMouseDragged

    private void btnAbrActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAbrActionPerformed
        cargarDic();
    }//GEN-LAST:event_btnAbrActionPerformed

    private void btnOpeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOpeActionPerformed
        new Thread(new Runnable(){
            public void run(){
                Operar();
            }
        }).start();
        //Operar();
    }//GEN-LAST:event_btnOpeActionPerformed

    private void evtCerrar(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_evtCerrar
        System.exit(0); 
    }//GEN-LAST:event_evtCerrar

    private void evtMinimizar(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_evtMinimizar
        this.setExtendedState(this.CROSSHAIR_CURSOR);
    }//GEN-LAST:event_evtMinimizar

    private void txtDicMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtDicMouseReleased
        JFileChooser fc = new JFileChooser();
        int sel = fc.showOpenDialog(null);
        if(sel == JFileChooser.APPROVE_OPTION)
        {
            ubi = fc.getSelectedFile().toString().replace("\\", "/");
            txtDic.setText(ubi);
        }
        else
        {
            btnAbr.grabFocus();
        }
    }//GEN-LAST:event_txtDicMouseReleased

    private void btnLimActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLimActionPerformed
        Limpiar();
    }//GEN-LAST:event_btnLimActionPerformed

    private void evtAcercaDe(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_evtAcercaDe
        String acercade = 
        "Este programa fue creado para maximizar el rendimiento\n"
        + "a la hora de crackear un hash md5 por FB con diccionario,\n"
        + "ademas tambien es pensado para los que no tienen internet\n"
        + "en casa lo que hace que no se puedan usar paginas como\n"
        + "md5decrypt.com entre otras...\n\n"
        + "Espero haber sido de ayuda y ps que modifiquen el codigo\n"
        + "a su gusto, ademas de que recuerden tambien intentar usar\n"
        +"los mismos diccionarios pero en Mayus y Minus... ATT:\n"
        +"KidGoth";
        
        JOptionPane.showMessageDialog(null, acercade, "AcercaDe", JOptionPane.DEFAULT_OPTION);
    }//GEN-LAST:event_evtAcercaDe

    private void lblDesMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblDesMouseReleased
        JOptionPane.showMessageDialog(null, lblDes.getText(), "Hash Descifrado", JOptionPane.DEFAULT_OPTION);       // TODO add your handling code here:
    }//GEN-LAST:event_lblDesMouseReleased

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new frmIndex().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private org.edisoncor.gui.button.ButtonSeven btnAbr;
    private org.edisoncor.gui.button.ButtonSeven btnLim;
    private org.edisoncor.gui.button.ButtonSeven btnOpe;
    private javax.swing.JComboBox cbxOpc;
    private javax.swing.JLabel lblADe;
    private javax.swing.JLabel lblAn;
    private javax.swing.JLabel lblBor;
    private javax.swing.JLabel lblDes;
    private org.edisoncor.gui.label.LabelMetric lblDicc;
    private javax.swing.JLabel lblFondo;
    private org.edisoncor.gui.label.LabelMetric lblHash;
    private javax.swing.JLabel lblMin;
    private javax.swing.JLabel lblProc;
    private javax.swing.JLabel lblSal;
    private javax.swing.JLabel lblTit;
    private javax.swing.JLabel lblp;
    private org.edisoncor.gui.textField.TextFieldRoundIcon txtDic;
    private org.edisoncor.gui.textField.TextFieldRoundIcon txtHash;
    // End of variables declaration//GEN-END:variables
    
    public void Limpiar()
    {
        txtDic.setText("");
        txtHash.setText("");
        lblProc.setText("Esperando Inicio...");
        lblDes.setText("");
        ubi = "";
        txtHash.grabFocus();
        combo();
        cbxOpc.setVisible(false);
    }
    public void Operar()
    {
        hash = txtHash.getText();
        if((txtHash.getText().length()) != 32)
        {
            JOptionPane.showMessageDialog(null, "Hash invalido... Por favor verifiquelo y vuelva a intentarlo.", "Hash Invalido", JOptionPane.ERROR_MESSAGE);
            txtHash.grabFocus();
        }
        else if((txtDic.getText().length()) <= 3)
        {
            JOptionPane.showMessageDialog(null, "No ha Elegido un Diccionario...", "Sin Diccionario", JOptionPane.ERROR_MESSAGE);
        }
        else
        {
            Comprobar();            
            if(descifrado == 0)
            {
                lblDes.setText("No Encontrado...");
                JOptionPane.showMessageDialog(null, "Lo siento, con este diccionario no ha funcionado.\nPrueba con otro...", "Hash No Encontrado", JOptionPane.WARNING_MESSAGE);
                cbxOpc.setVisible(true);
            }
            else
            {
                JOptionPane.showMessageDialog(null, "Enhorabuena tu hash a sido descifrado xD...", "Hash Encontrado", JOptionPane.INFORMATION_MESSAGE);
                cbxOpc.setVisible(false);
            }            
        }
                
    }
    
    public void Comprobar()
    {
        String crypt = "";
        if(cbxOpc.getSelectedItem().equals("Mayusculas") && (cbxOpc.isVisible()) == true)
        {
            try
            {
                lblProc.setText("Descifrando en Mayusculas...");
                BufferedReader bf = new BufferedReader(new FileReader(ubi));
                cadena = "";
                while((cadena = bf.readLine()) != null)
                {
                    crypt = enc.Crypt(cadena.toUpperCase());
                    if(hash.equals(crypt))
                    {
                        lblDes.setText(cadena.toUpperCase());
                        descifrado = 1;
                        break;
                    }
                    else
                    {
                        lblDes.setText(cadena);     //Aqui es lo que quiero que haga
                    }
                }
            }
            catch(Exception ex)
            {
                JOptionPane.showMessageDialog(null, "Ha Ocurrido un error:\n" + ex + "\nultimo intento: " + cadena, "Error", JOptionPane.ERROR_MESSAGE);
            }
        }
        else if(cbxOpc.getSelectedItem().equals("Minusculas") && (cbxOpc.isVisible()) == true)
        {
            try
            {
                lblProc.setText("Descifrando en Minusculas...");
                BufferedReader bf = new BufferedReader(new FileReader(ubi));
                cadena = "";
                while((cadena = bf.readLine()) != null)
                {
                    crypt = enc.Crypt(cadena.toLowerCase());
                    if(hash.equals(crypt))
                    {
                        lblDes.setText(cadena.toLowerCase());
                        descifrado = 1;
                        break;
                    }
                    else
                    {
                        lblDes.setText(cadena);     //Aqui es lo que quiero que haga
                    }
                }
            }
            catch(Exception ex)
            {
                JOptionPane.showMessageDialog(null, "Ha Ocurrido un error:\n" + ex + "\nultimo intento: " + cadena, "Error", JOptionPane.ERROR_MESSAGE);
            }
        }
        else
        {
            try
            {
                lblProc.setText("Descifrando Normal...");
                BufferedReader bf = new BufferedReader(new FileReader(ubi));
                cadena = "";
                while((cadena = bf.readLine()) != null)
                {
                    crypt = enc.Crypt(cadena);
                    if(hash.equals(crypt))
                    {
                        lblDes.setText(cadena);
                        descifrado = 1;
                        break;
                    }
                    else
                    {
                        lblDes.setText(cadena);     //Aqui es lo que quiero que haga
                    }
                }
            }
            catch(Exception ex)
            {
                JOptionPane.showMessageDialog(null, "Ha Ocurrido un error:\n" + ex + "\nultimo intento: " + cadena, "Error", JOptionPane.ERROR_MESSAGE);
            }
        }       
    }
    
    public void combo()
    {
        cbxOpc.removeAllItems();
        cbxOpc.addItem("No Funciono??");
        cbxOpc.addItem("Mayusculas");
        cbxOpc.addItem("Minusculas");
        cbxOpc.setSelectedIndex(0);
    }
    
    public void cargarDic()
    {
        JFileChooser fc = new JFileChooser();
        int sel = fc.showOpenDialog(null);
        if(sel == JFileChooser.APPROVE_OPTION)
        {
            ubi = fc.getSelectedFile().toString().replace("\\", "/");
            if(ubi.contains("."))
            {
                subExt = ubi.substring(ubi.lastIndexOf("."), ubi.length());
                if(!(subExt.equals(".txt")) && !(subExt.equals(".rtf")))
                {
                    JOptionPane.showMessageDialog(null, "Solo se aceptan archivos de texto plano sin formato, .txt o .rtf", "Archivo invalido", JOptionPane.WARNING_MESSAGE);
                    txtDic.setText("");
                }
                else
                {
                    txtDic.setText(ubi);
                }
            }
            else
            {
                txtDic.setText(ubi);
            }           
        }
        else
        {
            btnAbr.grabFocus();
        }
    }

}
